package cn.com.surker.sale.service.impl;

import cn.com.surker.order.entity.WechatOrder;
import cn.com.surker.sale.entity.SaleClient;
import cn.com.surker.sale.dao.SaleClientDao;
import cn.com.surker.sale.service.SaleClientService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * <p>
 * 销售客户表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-06-18
 */
@Service
public class SaleClientServiceImpl extends ServiceImpl<SaleClientDao, SaleClient> implements SaleClientService {

	@Resource
	private SaleClientDao saleClientDao;


	@Override
	public SaleClient queryByTaxCode(String taxCode) {
		QueryWrapper<SaleClient> qw = new QueryWrapper<>();
		qw.eq("tax_code", taxCode);
		List<SaleClient> saleClients = saleClientDao.selectList(qw);
		if (saleClients != null && saleClients.size() > 0) {
			SaleClient saleClient = saleClients.get(0);
			return saleClient;
		}
		return null;
	}

	@Override
	public SaleClient queryByName(String name) {
		QueryWrapper<SaleClient> qw = new QueryWrapper<>();
		qw.eq("name", name);
		List<SaleClient> saleClients = saleClientDao.selectList(qw);
		if (saleClients != null && saleClients.size() > 0) {
			SaleClient saleClient = saleClients.get(0);
			return saleClient;
		}
		return null;
	}

	@Override
	public SaleClient queryClient(String name, String fullName, String taxCode, String linkMan, Integer status) {

		List<SaleClient> saleClients = queryList(name, fullName, taxCode, linkMan, null, status);
		if (saleClients != null && saleClients.size() > 0) {
			saleClients.get(0);
		}
		return null;
	}

	@Override
	public List<SaleClient> queryList(String name, String fullName, String taxCode, String linkMan, Long orgId, Integer status) {
		QueryWrapper<SaleClient> qw = new QueryWrapper<>();
		if (StringUtils.isNotBlank(name)) {
			qw.like("name", name);
		}
		if (StringUtils.isNotBlank(fullName)) {
			qw.like("full_name", fullName);
		}
		if (StringUtils.isNotBlank(taxCode)) {
			qw.like("tax_code", taxCode);
		}
		if (StringUtils.isNotBlank(linkMan)) {
			qw.like("link_name", linkMan);
		}
		if (orgId != null) {
			qw.inSql("id", "select client_id from sale_client_org where status = 0 and org_id = " + orgId);
		}
		if (status != null) {
			qw.eq("status", status);
		}
		List<SaleClient> saleClients = saleClientDao.selectList(qw);
		return saleClients;
	}

	@Override
	public IPage<SaleClient> queryPage(String name, String fullName, String taxCode, String linkMan, Integer status, Integer offset, Integer limit) {
		QueryWrapper<SaleClient> qw = new QueryWrapper<>();
		if (StringUtils.isNotBlank(name)) {
			qw.like("name", name);
		}
		if (StringUtils.isNotBlank(fullName)) {
			qw.like("full_name", fullName);
		}
		if (StringUtils.isNotBlank(taxCode)) {
			qw.like("tax_code", taxCode);
		}
		if (StringUtils.isNotBlank(linkMan)) {
			qw.like("link_name", linkMan);
		}
		if (status != null) {
			qw.eq("status", status);
		}
		PageHelper.offsetPage(offset, limit);
		IPage<SaleClient> iPage = saleClientDao.selectPage(new Page<>((offset + limit) / limit, limit), qw);
		/**分页插件**/
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}
}